package io.mft.commons.ioc;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.AnnotatedBeanDefinition;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;

/**
 * 
 * @类型名称：BeanBeforeInitializationProcessorRegistry
 * @说明：TODO
 * @创建者: 胡常建
 * @创建时间: 2018年3月7日 下午11:54:33
 * @修改者: 胡常建
 * @修改时间: 2018年3月7日 下午11:54:33
 */
@SpringIocRegistry
public class BeanBeforeInitializationProcessorRegistry extends BaseRegistry{
	private final static  List<BeanBeforeInitializationProcessor<?>> processors=new ArrayList<BeanBeforeInitializationProcessor<?>>();
	public static List<BeanBeforeInitializationProcessor<?>> getProcessors() {
		return processors;
	}
	public BeanBeforeInitializationProcessorRegistry(String sysNo,String basePackage) {
		super(sysNo, basePackage, BeanBeforeInitialization.class);
	}
	@Override
	protected boolean isCandidateComponent(AnnotatedBeanDefinition beanDefinition) {
		return beanDefinition.getMetadata().isConcrete();
	}

	public List<BeanBeforeInitializationProcessor<?>> load() {
		List<BeanBeforeInitializationProcessor<?>> processors=new ArrayList<BeanBeforeInitializationProcessor<?>>();
		Set<BeanDefinition> beanDefinitions=findCandidateComponents(basePackage);
		for (BeanDefinition definition : beanDefinitions) {
			try {
				String beanClassName=definition.getBeanClassName();
				Class<?> class1=Class.forName(beanClassName);
				BeanBeforeInitializationProcessor<?> processor=(BeanBeforeInitializationProcessor<?>)class1.newInstance();
				processors.add(processor);
				getProcessors().add(processor);
			//	logger.info("加载BeanBeforeInitializationProcessor初始化处理器："+beanClassName);
			} catch (Exception e) {
				logger.error("", e);
			}
		}
		return processors;
	}

	@Override
	public void reg(BeanDefinitionRegistry registry) {
		load();
	}
}
